﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Configuration;

namespace MegauploadForum
{
    public partial class Form1 : Form
    {
        #region Members
        
        private LinkParserManager m_parserManager;
        private LinksFilter m_filter;
        private StringBuilder m_resultString;

        #endregion

        #region Ctor

        public Form1()
        {
            InitializeComponent();

            m_resultString = new StringBuilder();
            m_filter = new LinksFilter();
            m_parserManager = new LinkParserManager();
            m_parserManager.WaitBetweenTwoLinks = TimeSpan.Parse(ConfigurationManager.AppSettings["WaitingTime"]);
            m_parserManager.OnParsed += parser_OnParsed;

            IList<WebBrowser> webBrowsers = new List<WebBrowser>();
            int numOfWebBrowser = int.Parse(ConfigurationManager.AppSettings["PoolSize"]);
            for (int i = 0; i < numOfWebBrowser; i++)
            {
                webBrowsers.Add(CreateNewWebBrowser());
            }

            m_parserManager.Init(webBrowsers);
        }

        #endregion

        #region Private Methods

        private void parser_OnParsed(object sender, ParsingResultEventArgs e)
        {
            m_resultString.AppendLine("*******************************************");

            StringBuilder curResult = new StringBuilder();
            foreach (string link in e.Result.Links)
            {
                curResult.AppendLine(link);
            }

            string result = curResult.ToString();
            


            CopyToClipboard(result);
            m_resultString.Append(result);

            m_resultString.AppendLine("\n*******************************************");


            DispalyResultText();
        }

        private void CopyToClipboard(string data)
        {
            Invoke(new MethodInvoker(() => Clipboard.SetText(data)));
        }

        private void DispalyResultText()
        {
            txtResult.Invoke(new MethodInvoker(() => txtResult.Text = m_resultString.ToString()));
        }

        private WebBrowser CreateNewWebBrowser()
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.Visible = false;
            this.Controls.Add(webBrowser);
            return webBrowser;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string[] links = m_filter.Parse(txtRequest.Text);

                m_resultString.AppendLine("\nStart new request");
                DispalyResultText();

                m_parserManager.ParseRequest(new ParsingRequest { Links = links });
            }
            catch (Exception)
            {
                m_resultString.AppendLine("\nThere is error with input");
                DispalyResultText();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_parserManager.Dispose();
        }

        #endregion
    }
}
